package manage.controller;	
	
import manage.dao.UserMapper;
import manage.model.Pay;
import manage.model.User;
import manage.service.impl.PayServiceImpl;
import manage.util.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;	
import org.springframework.web.bind.annotation.RequestMethod;	
import org.springframework.web.bind.annotation.RequestParam;	
import org.springframework.web.bind.annotation.ResponseBody;	
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manage.model.Pay;
import manage.model.Wage;
import manage.service.PayService;
import manage.base.BaseController;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
	
@Controller
public class PayController extends BaseController {
	@Autowired	
	private PayServiceImpl payService;
	@Autowired
	private UserMapper userMapper;

	private User user;
	//返回Pay的list
	@RequestMapping(value = "/pay")
	@ResponseBody
	public Msg PayData(HttpServletRequest request, Authentication authentication){
		String page = request.getParameter("currentPage");
		String limit = request.getParameter("pageSize");
		UserDetails userDetails =(UserDetails) authentication.getPrincipal();
		user = userMapper.selectByPrimaryKey(userDetails.getUsername());
		//判断是否为普通用户或者管理员
		if(user.getAuthority().equals("2")){
			return Msg.success().add("data",payService.selectAll(page,limit,user.getAccount()));
		}
		return Msg.success().add("data",payService.selectAll(page,limit));
	}

	@ResponseBody
	@RequestMapping(value = "/addPay")
	public Msg updateWage(FSIAOHF obj){
		PayUtil.setFSIAOHF(obj);
		return Msg.attendanceAdd("success");
	}

	//根据主键查Pay 和修改配合使用
	@ResponseBody
	@RequestMapping(value = "/payM")
	public Msg selPay(@RequestBody Integer id){
		Pay obj = payService.select(id);
		obj.setBooleanpay("是");
		payService.update(obj);
		LogUtil.writeFileLog(user.getAccount(),"发放工资:"+obj.toString());
		return Msg.success();
	}

	@ResponseBody
	@RequestMapping(value = "/paySelect")
	public Msg selWage(){
		FSIAOHF fsiaohf=PayUtil.getFSIAOHF();
		return Msg.success().add("pay",fsiaohf);
	}

	/**
	 * 导出报表* @return
	 */
	@RequestMapping("/exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletRequest request,HttpServletResponse response){
		try {
			//获取数据源
			List<Pay> payList = payService.selectAll();

			//导出excel
			response.setHeader("Content-Disposition", "attachment;filename=" + new String("payInfo".getBytes(), "iso-8859-1") + ".xls");
			response.setContentType("application/x-excel;charset=UTF-8");
			OutputStream outputStream = response.getOutputStream();
			//导出
			payService.exportExcel(payList,outputStream);
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}	
